package mymyoux.io.mserverconnectorClasses
{
	import mymyoux.events.HashEvent;
	import mymyoux.events.MEventDispatcher;
	import mymyoux.events.MPlaylistEvent;
	import mymyoux.utils.Hash;
	public class MFilteredPlaylist extends MEventDispatcher
	{
		protected var _playlist:MRoomPlaylist;
		protected var _filters:Hash;
		protected var _players:Hash;
		public function MFilteredPlaylist(playlist:MRoomPlaylist,filters:Hash=null)
		{
			_playlist=playlist;
			_players=new Hash();
			_playlist.addEventListener(MPlaylistEvent.PEOPLE_CHANGE_INFO,onPeople);
			_playlist.addEventListener(MPlaylistEvent.PEOPLE_IN,onPeople);
			_playlist.addEventListener(MPlaylistEvent.PEOPLE_OUT,onPeople);
			this.filters=filters!=null?filters:new Hash();
		}
		public function get playlist():MRoomPlaylist{
			return _playlist;
		}
		public function get filters():Hash{
			return _filters;
		}
		public function set filters(value:Hash):void{
			if(_filters==null){
				if(value!=null){
					_filters=value;
					_filters.addEventListener(HashEvent.UPDATE,onFilterChange);
					_filters.addEventListener(HashEvent.REMOVE,onFilterChange);
					onFilterChange();
				}
			}else{
				if(_filters.equals(value)){
					onFilterChange();
				}else{
					_filters.destroy();
					_filters=value;
					if(value!=null){
						_filters.addEventListener(HashEvent.UPDATE,onFilterChange);
						_filters.addEventListener(HashEvent.REMOVE,onFilterChange);
					}
					onFilterChange();
				}
			
			}
		}	
		protected function onFilterChange(e:HashEvent=null):void{
				_playlist.removeEventListener(MPlaylistEvent.PEOPLE_CHANGE_INFO,onPeople);
				_playlist.removeEventListener(MPlaylistEvent.PEOPLE_IN,onPeople);
				_playlist.removeEventListener(MPlaylistEvent.PEOPLE_OUT,onPeople);
				_playlist.addEventListener(MPlaylistEvent.PEOPLE_CHANGE_INFO,onPeople);
				_playlist.addEventListener(MPlaylistEvent.PEOPLE_IN,onPeople);
				_playlist.addEventListener(MPlaylistEvent.PEOPLE_OUT,onPeople);
		}
		protected function onPeople(e:MPlaylistEvent):void{
			var accepted:Boolean=true;
			if(filters!=null){
				var i:int;
				var keys:Array=filters.getKeys();
				for(i=0;i<keys.length;i++){
					if(!e.people.hasValue(keys[i])){
						accepted=false;
						break;
					}else{
						if(filters.get(keys[i]).length>0){
							if(e.people.getValue(keys[i])!=filters.get(keys[i])){
								accepted=false;
								break;
							}
						}
					}
				}
			}
			if(accepted){
				if(!_players.containsKey(e.people.idx)){
					_players.put(e.people.idx,e.people);
					if(e.type!=MPlaylistEvent.PEOPLE_IN){
						dispatchEvent(new MPlaylistEvent(MPlaylistEvent.PEOPLE_IN,e.people,e.bubbles,e.cancelable));
					}
				}
				dispatchEvent(new MPlaylistEvent(e.type,e.people,e.bubbles,e.cancelable));
				if(e.type==MPlaylistEvent.PEOPLE_OUT){
					_players.remove(e.people.idx);
				}
			}else{
				if(_players.containsKey(e.people.idx)){
					dispatchEvent(new MPlaylistEvent(MPlaylistEvent.PEOPLE_OUT,e.people,e.bubbles,e.cancelable));
					_players.remove(e.people.idx);
				}
			}
		}
		public function containsPeople(people:MServerIdentifier):Boolean{
				return _players!=null?_players.containsKey(people.idx):false;
		}
		public function getPeople(idx:int):MServerIdentifier{
			if(_players.containsKey(idx)){
				return _players.get(idx) as MServerIdentifier;
			}
			return null;
		}
		public override function destroy():void{
				_playlist.removeEventListener(MPlaylistEvent.PEOPLE_CHANGE_INFO,onPeople);
				_playlist.removeEventListener(MPlaylistEvent.PEOPLE_IN,onPeople);
				_playlist.removeEventListener(MPlaylistEvent.PEOPLE_OUT,onPeople);
				_filters.destroy();
				super.destroy();
		}
		public override function get length():uint{
			return _players!=null?_players.length:0;
		}
		public function get players():Hash{
			return _players;
		}
		public function get snapshot():Hash{
			return _players.clone();
		}
	}
}